import { defineStore } from 'pinia'
import api from '@/api'

export const useHRStore = defineStore('hr', {
  state: () => ({
    employees: [],
    attendanceRecords: [],
    payrollRecords: []
  }),

  getters: {
    // 获取员工列表
    getEmployees: (state) => state.employees,
    
    // 获取考勤记录列表
    getAttendanceRecords: (state) => state.attendanceRecords,
    
    // 获取薪资记录列表
    getPayrollRecords: (state) => state.payrollRecords
  },

  actions: {
    // 获取员工列表
    async fetchEmployees() {
      try {
        const response = await api.get('/api/hr/employees')
        if (response.data.code === 200) {
          this.employees = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取员工列表失败:', error)
        throw error
      }
    },

    // 创建员工
    async createEmployee(employeeData) {
      try {
        const response = await api.post('/api/hr/employees', employeeData)
        if (response.data.code === 200) {
          // 添加到本地状态
          this.employees.push(response.data.data)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('创建员工失败:', error)
        throw error
      }
    },

    // 更新员工
    async updateEmployee(id, employeeData) {
      try {
        const response = await api.put(`/api/hr/employees/${id}`, employeeData)
        if (response.data.code === 200) {
          // 更新本地状态
          const index = this.employees.findIndex(employee => employee.id === id)
          if (index !== -1) {
            this.employees[index] = response.data.data
          }
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('更新员工失败:', error)
        throw error
      }
    },

    // 删除员工
    async deleteEmployee(id) {
      try {
        const response = await api.delete(`/api/hr/employees/${id}`)
        if (response.data.code === 200) {
          // 从本地状态移除
          this.employees = this.employees.filter(employee => employee.id !== id)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('删除员工失败:', error)
        throw error
      }
    },

    // 获取考勤记录列表
    async fetchAttendanceRecords() {
      try {
        const response = await api.get('/api/hr/attendance')
        if (response.data.code === 200) {
          this.attendanceRecords = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取考勤记录列表失败:', error)
        throw error
      }
    },

    // 创建考勤记录
    async createAttendanceRecord(attendanceData) {
      try {
        const response = await api.post('/api/hr/attendance', attendanceData)
        if (response.data.code === 200) {
          // 添加到本地状态
          this.attendanceRecords.push(response.data.data)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('创建考勤记录失败:', error)
        throw error
      }
    },

    // 更新考勤记录
    async updateAttendanceRecord(id, attendanceData) {
      try {
        const response = await api.put(`/api/hr/attendance/${id}`, attendanceData)
        if (response.data.code === 200) {
          // 更新本地状态
          const index = this.attendanceRecords.findIndex(record => record.id === id)
          if (index !== -1) {
            this.attendanceRecords[index] = response.data.data
          }
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('更新考勤记录失败:', error)
        throw error
      }
    },

    // 删除考勤记录
    async deleteAttendanceRecord(id) {
      try {
        const response = await api.delete(`/api/hr/attendance/${id}`)
        if (response.data.code === 200) {
          // 从本地状态移除
          this.attendanceRecords = this.attendanceRecords.filter(record => record.id !== id)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('删除考勤记录失败:', error)
        throw error
      }
    },

    // 获取薪资记录列表
    async fetchPayrollRecords() {
      try {
        const response = await api.get('/api/hr/payroll')
        if (response.data.code === 200) {
          this.payrollRecords = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取薪资记录列表失败:', error)
        throw error
      }
    },

    // 创建薪资记录
    async createPayrollRecord(payrollData) {
      try {
        const response = await api.post('/api/hr/payroll', payrollData)
        if (response.data.code === 200) {
          // 添加到本地状态
          this.payrollRecords.push(response.data.data)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('创建薪资记录失败:', error)
        throw error
      }
    },

    // 更新薪资记录
    async updatePayrollRecord(id, payrollData) {
      try {
        const response = await api.put(`/api/hr/payroll/${id}`, payrollData)
        if (response.data.code === 200) {
          // 更新本地状态
          const index = this.payrollRecords.findIndex(record => record.id === id)
          if (index !== -1) {
            this.payrollRecords[index] = response.data.data
          }
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('更新薪资记录失败:', error)
        throw error
      }
    },

    // 删除薪资记录
    async deletePayrollRecord(id) {
      try {
        const response = await api.delete(`/api/hr/payroll/${id}`)
        if (response.data.code === 200) {
          // 从本地状态移除
          this.payrollRecords = this.payrollRecords.filter(record => record.id !== id)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('删除薪资记录失败:', error)
        throw error
      }
    }
  }
})